package com.jxb.myshop.services.impl;

import com.jxb.myshop.common.Constant;
import com.jxb.myshop.common.ResponseResult;
import com.jxb.myshop.dao.CartDao;
import com.jxb.myshop.dao.ItemDao;
import com.jxb.myshop.dao.OrderDao;
import com.jxb.myshop.dao.impl.CartDaoImpl;
import com.jxb.myshop.dao.impl.ItemDaoImpl;
import com.jxb.myshop.dao.impl.OrderDaoImpl;
import com.jxb.myshop.entity.Cart;
import com.jxb.myshop.entity.Item;
import com.jxb.myshop.entity.Orders;
import com.jxb.myshop.entity.vo.CartVo;
import com.jxb.myshop.entity.vo.OrderVo;
import com.jxb.myshop.services.OrderServices;
import com.jxb.myshop.util.RandomUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class OrderServicesImpl implements OrderServices {
    private OrderDao od = new OrderDaoImpl();
    private CartDao cd = new CartDaoImpl();
    private ItemDao id = new ItemDaoImpl();

    @Override
    public ResponseResult createOrder(int aid, int uid) {
        try {
            // 1.创建订单实体类
            Orders order = new Orders();
            // 2.设置订单的数据
            order.setAid(aid);
            order.setUid(uid);
            order.setOtime(new Date());
            order.setOid(RandomUtils.createOrderId());
            order.setOstate(Constant.Order.NOT_PAY);
            // 3.通过用户id获取购物车商品信息
            List<CartVo> cartList = cd.getCartWithProduct(uid);
            // 判断cartList是否有值
            if (cartList != null && cartList.size() > 0) {
                // 4.创建一个item集合用于保存购物车的信息
                List<Item> itemList = new ArrayList<>();
                // 5.创建一个sum用于保存总金额
                double sum = 0;
                for (CartVo cartVo : cartList) {
                    Item item = new Item(order.getOid(),cartVo.getPid(),cartVo.getCcount(),cartVo.getCnum());
                    sum += cartVo.getCcount();
                    // 把每个item数据加入集合
                    itemList.add(item);
                }
                order.setOcount(sum);
                // 6.创建一个订单
                od.createOrder(order);
                // 7.创建一个物品列表
                id.createItem(itemList);
                // 8.清除购物车内的物品
                cd.clearAll(uid);
                return ResponseResult.createSuccess("订单生成成功");
            }

        } catch (Exception e) {

        }
        return ResponseResult.createFail("创建订单失败");
    }

    @Override
    public ResponseResult getAllOrders(int uid) {
        try {
            List<OrderVo> orderList =  od.getAllOrders(uid);
            return ResponseResult.createSuccess(orderList);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ResponseResult.createFail("获取订单失败");
    }

    @Override
    public OrderVo getOrderAndAddress(String oid) {
        try {
            OrderVo ov = od.getOrderAndAddress(oid);
            if (ov != null) {
                return ov;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void changeStatus(String out_trade_no, int isSend) {
        System.out.println(isSend);
        try {
            OrderVo oa = od.getOrderAndAddress(out_trade_no);
            System.out.println(oa.getOstate());
            if (oa.getOstate() == Constant.Order.NOT_PAY) {
                od.changeStatus(out_trade_no, isSend);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void changeStatus(String oid) {
        try {
            od.changeStatus(oid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
